package action.creditapproval;

import java.util.List;
import model.credit.Creditapplyenterprise;
import model.credit.Creditapplypersonal;
import service.applycheck.ApplyOpinionService;
import service.applycheck.ApplyStateService;
import service.applycheck.ApplyUserService;
import service.credit.CreditApplyEnterpriseService;
import service.credit.CreditApplyPersonalService;
import util.Log;
import vo.CreditCheckBean;

import core.action.BaseAction;
import exception.Exceptions;

public class CreditAcceptAction extends BaseAction{
	
	private String state;
	private int type;
	private List<?> creditAcceptBeanList;
	private CreditCheckBean creditCheckBean;
	
	//审批记录表
	private List<?> recordList;
	private ApplyOpinionService applyOpinionService;
	
	private List<?> userList;
	private ApplyUserService applyUserService;
	
	private List<Integer> isAccept;
	private List<Integer> acceptPeople;
	private Integer isDone;
	
	private Integer applyid;
	
	private ApplyStateService applyStateService;
	
	private int searchwhat;
	
	private String searchApplyid;
	
	public CreditCheckBean getCreditCheckBean() {
		return creditCheckBean;
	}
	public void setCreditCheckBean(CreditCheckBean creditCheckBean) {
		this.creditCheckBean = creditCheckBean;
	}

	private CreditApplyPersonalService creditApplyPersonalService;
	private CreditApplyEnterpriseService creditApplyEnterpriseService;
	
	public String execute() throws Exceptions{
		if(this.type == 1){
			this.creditAcceptBeanList = this.creditApplyPersonalService.getAllAcceptionList("wait");
		}else if(this.type == 2){
			this.creditAcceptBeanList = this.creditApplyEnterpriseService.getAllAcceptionList("wait");
		}
		if(this.creditAcceptBeanList == null){
			this.isDone = 2;
		}else{
			this.userList = this.applyUserService.getUserByRoleId(4);
		}
		this.searchwhat = 1;
		return "success";
	}
	public String applyAccept() throws Exceptions{
		if (this.type == 1) {
			creditCheckBean = this.creditApplyPersonalService.getOneBean(applyid);
		} else if (this.type == 2) {
			creditCheckBean = this.creditApplyEnterpriseService.getOneBean(applyid);
		}
		recordList = this.applyOpinionService.getListAllByApplyId(applyid);
		return "detail";
	}
	
	public String cdw() throws Exceptions{
		if(this.type == 1){
			this.creditAcceptBeanList = this.creditApplyPersonalService.getAllAcceptionList("wait");
		}else if(this.type == 2){
			this.creditAcceptBeanList = this.creditApplyEnterpriseService.getAllAcceptionList("wait");
		}
		if(this.creditAcceptBeanList == null){
			this.isDone = 2;
		}else{
			this.userList = this.applyUserService.getUserByRoleId(4);
		}
		this.searchwhat = 1;
		return "success";
	}
	public String cdd() throws Exceptions{
		if(this.type == 1){
			this.creditAcceptBeanList = this.creditApplyPersonalService.getAllAcceptionList("done");
		}else if(this.type == 2){
			this.creditAcceptBeanList = this.creditApplyEnterpriseService.getAllAcceptionList("done");
		}
		this.searchwhat = 2;
		this.isDone = 2;
		return "success";
	}
	public String doAccept() throws Exception{
		String peopelType = "";
		
		for(Integer aid : this.isAccept){
			for(Integer people : this.acceptPeople){
				try {
					this.applyStateService.saveAllByManagerId(aid, people, "A", this.type, 1);
					if(this.type == 1){
						Creditapplypersonal cap = this.creditApplyPersonalService.getOneById(aid);
						cap.setCreditstate(11);
						cap.setCount(1);
						this.creditApplyPersonalService.save(cap);
						peopelType = "个人";
					}else if(this.type == 2){
						Creditapplyenterprise cae = this.creditApplyEnterpriseService.getOneById(aid);
						cae.setCreditstate(11);
						cae.setCount(1);
						this.creditApplyEnterpriseService.save(cae);
						peopelType = "企业";
					}
				} catch (Exception e) {
					throw new Exceptions("授理过程出现异常！");
				}
			}
		}
		Log.save("插入", peopelType + "授理，分配调查人员，影响的申请id为：" + isAccept + "，分配的人员为：" + acceptPeople, null);
		MessageJump("申请授理完成", "creditAcceptAction?type=" + this.type);
		return "message";
	}
	
	public String searchList() throws Exceptions{
		System.out.println("id = " + this.searchApplyid + ", type = " + this.type);
		
		try{
			this.applyid = Integer.parseInt(this.searchApplyid);
		}catch(Exception e){
			this.applyid = Integer.parseInt(this.searchApplyid.substring(3, this.searchApplyid.length()));
		}
		
		if(this.type == 1){
			this.creditAcceptBeanList = this.creditApplyPersonalService.getAllAcceptionListByApplyIdAndState(this.applyid, "accept");
		}else if(this.type == 2){
			this.creditAcceptBeanList = this.creditApplyEnterpriseService.getAllAcceptionListByApplyIdAndState(this.applyid, "accept");
		}
		if(this.creditAcceptBeanList == null){
			this.isDone = 2;
		}else{
			this.userList = this.applyUserService.getUserByRoleId(4);
		}
		return "success";
	}
	
	public List<?> getCreditAcceptBeanList() {
		return creditAcceptBeanList;
	}
	public void setCreditAcceptBeanList(List<?> creditAcceptBeanList) {
		this.creditAcceptBeanList = creditAcceptBeanList;
	}
	public CreditApplyPersonalService getCreditApplyPersonalService() {
		return creditApplyPersonalService;
	}
	public void setCreditApplyPersonalService(
			CreditApplyPersonalService creditApplyPersonalService) {
		this.creditApplyPersonalService = creditApplyPersonalService;
	}
	public CreditApplyEnterpriseService getCreditApplyEnterpriseService() {
		return creditApplyEnterpriseService;
	}
	public void setCreditApplyEnterpriseService(
			CreditApplyEnterpriseService creditApplyEnterpriseService) {
		this.creditApplyEnterpriseService = creditApplyEnterpriseService;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	public Integer getApplyid() {
		return applyid;
	}
	public void setApplyid(Integer applyid) {
		this.applyid = applyid;
	}
	public List<Integer> getAcceptPeople() {
		return acceptPeople;
	}
	public void setAcceptPeople(List<Integer> acceptPeople) {
		this.acceptPeople = acceptPeople;
	}
	public ApplyStateService getApplyStateService() {
		return applyStateService;
	}
	public void setApplyStateService(ApplyStateService applyStateService) {
		this.applyStateService = applyStateService;
	}
	public void setIsAccept(List<Integer> isAccept) {
		this.isAccept = isAccept;
	}
	public ApplyUserService getApplyUserService() {
		return applyUserService;
	}
	public void setApplyUserService(ApplyUserService applyUserService) {
		this.applyUserService = applyUserService;
	}
	public List<Integer> getIsAccept() {
		return isAccept;
	}
	public List<?> getUserList() {
		return userList;
	}
	public void setUserList(List<?> userList) {
		this.userList = userList;
	}
	public Integer getIsDone() {
		return isDone;
	}
	public void setIsDone(Integer isDone) {
		this.isDone = isDone;
	}
	public int getSearchwhat() {
		return searchwhat;
	}
	public void setSearchwhat(int searchwhat) {
		this.searchwhat = searchwhat;
	}
	public String getSearchApplyid() {
		return searchApplyid;
	}
	public void setSearchApplyid(String searchApplyid) {
		this.searchApplyid = searchApplyid;
	}
	public List<?> getRecordList() {
		return recordList;
	}
	public void setRecordList(List<?> recordList) {
		this.recordList = recordList;
	}
	public ApplyOpinionService getApplyOpinionService() {
		return applyOpinionService;
	}
	public void setApplyOpinionService(ApplyOpinionService applyOpinionService) {
		this.applyOpinionService = applyOpinionService;
	}
}
